package weeny.pre.renderer;

import java.awt.Graphics2D;
import java.awt.Shape;
import java.awt.geom.GeneralPath;
import java.awt.geom.Point2D;

import prefuse.Constants;
import prefuse.render.AbstractShapeRenderer;
import prefuse.render.EdgeRenderer;
import prefuse.visual.VisualItem;

public class LinkRenderer extends AbstractShapeRenderer{
	public static String GP_FLD = "generalPath_field";
	private EdgeRenderer  edgeRen = new EdgeRenderer();
	private AbstractShapeRenderer polyRen = new AbstractShapeRenderer(){
		@Override
		protected Shape getRawShape(VisualItem item) {
			return (GeneralPath) item.get(generalPathField);
		}
		@Override
		public int getRenderType(VisualItem item) {
			return RENDER_TYPE_DRAW;
		}
	};
	private String generalPathField = null;
	public void setEdgeArrowType(int type){
		edgeRen.setArrowType(type);
	}
	public LinkRenderer(){
		this(GP_FLD);
	}
	public LinkRenderer(String generalPathField) {
		this.generalPathField = generalPathField;
		edgeRen.setArrowType(Constants.EDGE_ARROW_NONE);
	}
	private boolean isRenderedAsEdge(VisualItem item){
		return !item.canGet(generalPathField, GeneralPath.class)
		       || item.get(generalPathField) == null;
	}
	@Override
	public boolean locatePoint(Point2D p, VisualItem item) {
		if (isRenderedAsEdge(item)){
			return edgeRen.locatePoint(p, item);
		}
		else{
			return polyRen.locatePoint(p, item);
		}
	}
	@Override
	public void setBounds(VisualItem item) {
		if (isRenderedAsEdge(item)){
			edgeRen.setBounds(item);
		}
		else{
			polyRen.setBounds(item);
		}
	}
	@Override
	protected Shape getRawShape(VisualItem item) {
		if (isRenderedAsEdge(item)){
			return edgeRen.getShape(item);
		}
		else{
			return polyRen.getShape(item);
		}
	}
	@Override
	public void render(Graphics2D g, VisualItem item) {
		if (isRenderedAsEdge(item)){
			edgeRen.render(g, item);
		}
		else{
			polyRen.render(g, item);
		}
	}
}
